﻿namespace Mozi.Live.SDP
{
    /// <summary>
    /// SDP属性
    /// </summary>
    public class SDPAttribute
    {
        /// <summary>
        /// 属性类型
        /// </summary>
        public SDPAttributeType Attribute { get; set; }
        /// <summary>
        /// 属性值
        /// </summary>
        public object Value { get; set; }
        /// <summary>
        /// 转为键值对表达式
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (string.IsNullOrEmpty(Attribute.Name))
            {
                return Value.ToString();
            }
            else
            {
                return $"{Attribute}:{Value}";
            }
        }

        public static SDPAttribute Parse(string data)
        {
            SDPAttribute att = new SDPAttribute();
            if (data.IndexOf(":") > 0)
            {
                var key = data.Substring(0, data.IndexOf(":"));
                var value = data.Substring(data.IndexOf(":") + 1);
                SDPAttributeType at = SDPAttributeType.Get<SDPAttributeType>(key);
                if (Equals(at,null))
                {
                    at = new SDPAttributeType(key);
                }
                att.Attribute = at;
                att.Value = value;
            }
            else
            {
                att.Attribute = SDPAttributeType.MediaDirection;
                att.Value = data;
            }
            return att;
        }
    }
    /// <summary>
    /// 媒体数据流向
    /// </summary>
    public enum SDPAttributeMediaDirection
    {
        RecvOnly = 1,
        SendRecv = 2,
        SendOnly = 3,
        InActive = 4
    }
}
